package com.xingchen.feima.star.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingchen.feima.common.utils.R;
import com.xingchen.feima.star.entity.Unit;
import com.xingchen.feima.star.service.UnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 机组表 前端控制器
 * </p>
 *
 * @author yangxc
 * @since 2025-02-10
 */
@Api(tags = "机组管理")
@RestController
@RequestMapping("/star/unit")
public class UnitController {

    @Autowired
    private UnitService unitService;


    @ApiOperation(value = "根据核电站id获取机组列表")
    @GetMapping("/getUnitListByNuclearPowerPlantId/{id}")
    public R getUnitListByNuclearPowerPlantId(
            @ApiParam(name = "id", value = "核电站id", required = true)
            @PathVariable Long id){

        List<Unit> unitListByNuclearPowerPlantId = unitService.list(new QueryWrapper<Unit>().eq("nuclear_power_plant_id", id));
        return R.ok().data("items", unitListByNuclearPowerPlantId);
    }

    @ApiOperation(value = "新增机组")
    @PostMapping("")
    public R add(
            @ApiParam(name = "unit", value = "机组对象", required = true)
            @RequestBody Unit unit){

        unitService.save(unit);
        return R.ok().message("添加成功");
    }

    @ApiOperation(value = "根据id查询机组")
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(name = "id", value = "机组id", required = true)
            @PathVariable Long id){

        Unit unit = unitService.getById(id);
        return R.ok().data("items", unit);
    }

    @ApiOperation(value = "根据id修改机组")
    @PutMapping("")
    public R updateById(
            @ApiParam(name = "unit", value = "机组对象", required = true)
            @RequestBody Unit unit){

        unitService.updateById(unit);
        return R.ok().message("修改成功");
    }

    @ApiOperation(value = "根据id删除机组")
    @DeleteMapping("/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "机组id", required = true)
            @PathVariable Long id){

        unitService.removeById(id);
        return R.ok().message("删除成功");
    }

    @ApiOperation("根据id列表批量删除机组")
    @DeleteMapping("/batchRemove")
    public R batchRemove(
            @ApiParam(name = "idList", value = "id列表", required = true)
            @RequestBody List<Long> idList) {

        unitService.removeByIds(idList);
        return R.ok().message("批量删除成功");
    }
}

